// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

import com.thalesgroup.preferenceFramework.models.LCPnet.LinguisticDomain;
import com.thalesgroup.preferenceFramework.models.LCPnet.LinguisticSubset;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetCombinationException;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.Fragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.FragmentsPackage;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LCPnetFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LinguisticDomainFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LinguisticSubsetFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.impl.LinguisticDomainImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Linguistic Domain Fragment</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LinguisticDomainFragmentImpl#getFragmentName <em>Fragment Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LinguisticDomainFragmentImpl#isAntiFragment <em>Anti Fragment</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.LinguisticDomainFragmentImpl#getBaseLinguisticDomain <em>Base Linguistic Domain</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class LinguisticDomainFragmentImpl extends LinguisticDomainImpl implements LinguisticDomainFragment {
	/**
	 * The default value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected static final String FRAGMENT_NAME_EDEFAULT = "";

	/**
	 * The cached value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected String fragmentName = FRAGMENT_NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ANTI_FRAGMENT_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected boolean antiFragment = ANTI_FRAGMENT_EDEFAULT;

	/**
	 * The cached value of the '{@link #getBaseLinguisticDomain() <em>Base Linguistic Domain</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBaseLinguisticDomain()
	 * @generated
	 * @ordered
	 */
	protected LinguisticDomain baseLinguisticDomain;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected LinguisticDomainFragmentImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return FragmentsPackage.Literals.LINGUISTIC_DOMAIN_FRAGMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getFragmentName() {
		return fragmentName;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFragmentName(String newFragmentName) {
		String oldFragmentName = fragmentName;
		fragmentName = newFragmentName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME, oldFragmentName, fragmentName));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isAntiFragment() {
		return antiFragment;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAntiFragment(boolean newAntiFragment) {
		boolean oldAntiFragment = antiFragment;
		antiFragment = newAntiFragment;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT, oldAntiFragment, antiFragment));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public LinguisticDomain getBaseLinguisticDomain() {
		if (baseLinguisticDomain != null && baseLinguisticDomain.eIsProxy()) {
			InternalEObject oldBaseLinguisticDomain = (InternalEObject)baseLinguisticDomain;
			baseLinguisticDomain = (LinguisticDomain)eResolveProxy(oldBaseLinguisticDomain);
			if (baseLinguisticDomain != oldBaseLinguisticDomain) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN, oldBaseLinguisticDomain, baseLinguisticDomain));
			}
		}
		return baseLinguisticDomain;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public LinguisticDomain basicGetBaseLinguisticDomain() {
		return baseLinguisticDomain;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBaseLinguisticDomain(LinguisticDomain newBaseLinguisticDomain) {
		LinguisticDomain oldBaseLinguisticDomain = baseLinguisticDomain;
		baseLinguisticDomain = newBaseLinguisticDomain;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN, oldBaseLinguisticDomain, baseLinguisticDomain));
	}

	public void applyFragment(EObject context) throws LCPnetCombinationException {
		if(context instanceof LCPnetFragment) {
			//Fragment contained in context --> apply fragment
			
			LinguisticDomain baseLinguisticDomain = this.getBaseLinguisticDomain();
			LCPnetFragment callingLCPnetFragment = (LCPnetFragment) context;
			
			if(callingLCPnetFragment.getUtilityDomain().equals(this)) {
				//Case 1: this fragment is an utility domain fragment --> deletion not allowed
			
				if(this.isAntiFragment()) {
					throw new LCPnetCombinationException("Usage error: deletion of the LCPNet Utility Domain not allowed");
				}
				
			} else {
				//Case 2: this fragment is a value domain fragment --> deletion allowed (because base nodes can be removed)
				
				if(this.isAntiFragment()) {
					callingLCPnetFragment.getBaseLCPnet().getValueDomains().remove(baseLinguisticDomain);
					return;
				}
			}
			
			//Warning: If it's an antiFragment, the following code will not be executed

			if(this.getName() != null && !this.getName().equals("")) {
				baseLinguisticDomain.setName(this.getName());
			}
			
			baseLinguisticDomain.setLowerBound(this.getLowerBound());
			baseLinguisticDomain.setUpperBound(this.getUpperBound());

			EList<LinguisticSubset> subsets = this.getSubsets();
			if(subsets != null) {
				//Avoid list concurrent modifications
				LinguisticSubset[] subsets_array = subsets.toArray(new LinguisticSubset[]{});

				for (LinguisticSubset linguisticSubset : subsets_array) {
					if(linguisticSubset instanceof LinguisticSubsetFragment) {
						//If LinguisticSubset fragment --> apply fragment
						((LinguisticSubsetFragment)linguisticSubset).applyFragment(this);
					} else {
						//If new LinguisticSubset --> add to domain LinguisticSubset list
						linguisticSubset.setDomain(baseLinguisticDomain);
					}
				}
			}
		} else if(context instanceof LNodeFragment) {
			//Fragment not contained in context

			throw new LCPnetCombinationException("Usage error: the value domain of an LNode can't be linked to a fragment");
		} else {
			throw new LCPnetCombinationException("Combination error: unexpected calling context for LinguisticDomain fragment");
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME:
				return getFragmentName();
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT:
				return isAntiFragment() ? Boolean.TRUE : Boolean.FALSE;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN:
				if (resolve) return getBaseLinguisticDomain();
				return basicGetBaseLinguisticDomain();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME:
				setFragmentName((String)newValue);
				return;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(((Boolean)newValue).booleanValue());
				return;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN:
				setBaseLinguisticDomain((LinguisticDomain)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME:
				setFragmentName(FRAGMENT_NAME_EDEFAULT);
				return;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(ANTI_FRAGMENT_EDEFAULT);
				return;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN:
				setBaseLinguisticDomain((LinguisticDomain)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME:
				return FRAGMENT_NAME_EDEFAULT == null ? fragmentName != null : !FRAGMENT_NAME_EDEFAULT.equals(fragmentName);
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT:
				return antiFragment != ANTI_FRAGMENT_EDEFAULT;
			case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__BASE_LINGUISTIC_DOMAIN:
				return baseLinguisticDomain != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (derivedFeatureID) {
				case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (baseFeatureID) {
				case FragmentsPackage.FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.LINGUISTIC_DOMAIN_FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (fragmentName: ");
		result.append(fragmentName);
		result.append(", antiFragment: ");
		result.append(antiFragment);
		result.append(')');
		return result.toString();
	}

} //LinguisticDomainFragmentImpl
